import Vue from "vue";
// 引入vuex
import Vuex from "vuex";

// 应用Vuex
Vue.use(Vuex);

// 创初始化仓库并暴露
export default new Vuex.Store({
  state: {
    count: 0,
    num: 0,
  },
  mutations: {
    /* mutations的方法会接受state作为第一个参数 */
    increament(state) {
      state.count++;
    },
    increamentN(state, payload) {
      state.count += payload.n;
    },
  },
  //   处理异步
  actions: {
    increamentWait({ commit }) {
      setTimeout(() => {
        commit("increament");
      }, 1000);
    },
    incrementNWait({ commit }, payload) {
      setTimeout(() => {
        commit("increamentN", payload);
      }, 1000);
    },
  },
  getters: {
    // getters其实就是组件内的计算属性值,根据state的值衍生出来另外一个值
    countSqrt(state) {
      return Math.sqrt(state.count);
    },
  },
});
